package com.example.demo02.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.demo02.common.ResponseEntity;
import com.example.demo02.dao.StatCouponShopMapper;
import com.example.demo02.dao.TestMapper;
import com.example.demo02.dto.ShopInfo;
import com.example.demo02.dto.excelBean.BlackSpecialCarExcelBean;
import com.example.demo02.entity.Person;
import com.example.demo02.entity.Shop;
import com.example.demo02.entity.StatCouponShop;
import com.example.demo02.entity.TcSpecialcar;
import com.example.demo02.exception.TestException;
import com.example.demo02.form.ShopForm;
import com.example.demo02.mapstruct.ExcelBeanToEntityStruct;
import com.example.demo02.server.WebSocketServer;
import com.example.demo02.service.StatCouponShopService;
import com.example.demo02.service.TestService;
import com.example.demo02.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class TestController {
    @Resource
    private TestMapper testMapper;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    StatCouponShopService statCouponShopService;
    @Resource
    StatCouponShopMapper statCouponShopMapper;
    @Resource
    TestService testService;



    @RequestMapping(value = "/getShopListByPage", method = RequestMethod.POST)
    public ResponseEntity<Object> getShopListByPage(@RequestBody ShopForm shopForm, HttpServletRequest httpRequest) {
        try {
            //master 111
            Assert.isTrue(ObjectUtil.isNotNull(shopForm.getOptunitid()),()-> new TestException(1,"运营id不可为空"));
            PageHelper.startPage(shopForm.getPageNum(),shopForm.getLength());
            List<Shop> list = testMapper.getShopListByPage(shopForm);
            PageInfo<Shop> of = PageInfo.of(list);
            Map<Integer,String> shopParkNameMap=getShopParkNameMap(list);
            Map<String,String> optunitNameMap=getOptunitNameMap(list);
            //master 222
            return ResponseEntity.success(PageUtils.mapping(of,node->ShopInfo.from(node,shopParkNameMap,optunitNameMap)));
        }catch (TestException e){
            return ResponseEntity.error(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.error("系统内部异常");
        }finally {
            PageHelper.clearPage();
        }
    }
    @GetMapping("/addShop")
    public String addShop(){
        try {
            WebSocketServer.sendMessage("添加商户成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return "添加成功";
    }

    @GetMapping("/testMybatis")
    public String testMybatis(){
        String start="20240403000000";
        String end="20240403235959";
        Integer count=testMapper.selectCountByDate(start,end);
        System.out.println(count);
        return "添加成功";
    }

    @GetMapping("/testMybatisUpdate")
    public String testMybatisUpdate(){
        statCouponShopService.updateUseCount();
        return "添加成功";
    }

    @GetMapping("/testMybatisQuery")
    public List<StatCouponShop> testMybatisQuery(){
        List<StatCouponShop> list= statCouponShopMapper.queryList();
        return list;
    }

    @GetMapping("/testRestart")
    public Person testRestart() {
        Person aaa = new Person().setAge(18).setName("aaa").setDay(new Date());
        return aaa;
    }

    @GetMapping("/testClass")
    public String testClass() {
        return "";
    }
    @Resource
    ExcelBeanToEntityStruct excelBeanToEntityStruct;

    @GetMapping("/testMapStruct")
    public String testMapStruct() {
        BlackSpecialCarExcelBean blackSpecialCarExcelBean = new BlackSpecialCarExcelBean();
        blackSpecialCarExcelBean.setPlateNo("1");
        blackSpecialCarExcelBean.setCreateTime(DateUtil.format(new Date(),"yyyy-MM-dd"));
        blackSpecialCarExcelBean.setRemark("111");
        TcSpecialcar tcSpecialcar = excelBeanToEntityStruct.toTcSpecialcar(blackSpecialCarExcelBean);
        System.out.println(tcSpecialcar);
        return "";
    }

    private Map<String, String> getOptunitNameMap(List<Shop> list) {
        Map<String,String> map=new HashMap<>();
        List<Integer> optids = list.stream().filter(ObjectUtil::isNotNull).map(Shop::getOptunitid).filter(ObjectUtil::isNotNull).collect(Collectors.toList());
        optids.forEach(optid->{
            String optName = Opt.ofNullable(testMapper.getOptNameById(optid)).get();
            map.put(optid.toString(),optName);
        });
        return map;
    }

    private Map<Integer, String> getShopParkNameMap(List<Shop> list) {
        Map<Integer,String> map=new HashMap<>();
        List<Integer> shopIds = list.stream().filter(ObjectUtil::isNotNull).map(Shop::getShopid).collect(Collectors.toList());
        shopIds.forEach(id->{
            String parkpotNames=testMapper.getParkpotNameByShopId(id);
            map.put(id, StrUtil.isBlank(parkpotNames)?"":parkpotNames);
        });
        return map;
    }
}
